home *** CD-ROM | disk | FTP | other *** search
/ Computer Inter@ctive 17 / Computer Interactive cdrom 17 - gen 99.iso / ZDNETIT / CONTENT / OPTIVC16.ZIP / DOC.ZIP / VDEMOW2.CPP < prev   
Encoding:
C/C++ Source or Header  |  1998-10-21  |  10.4 KB  |  260 lines

  1. /*****************************   VDEMOW2.CPP *****************************
  2. *                                                                        *
  3. *                Demo program for                                        *
  4. *               V e c t o r L i b                                        *
  5. *           for Borland C++ for Windows                                  *
  6. *     with OWL 2.0 or 5.0 (shipped with Borland C++ versions 4.x, 5.x)   *
  7. *                                                                        *
  8. *       Copyright 1996-1998 by Martin Sander                             *
  9. *                                                                        *
  10. *                                                                        *
  11. *       This sample program is meant to demonstrate how to use           *
  12. *       VectorLib within your Windows programs. It is not intended       *
  13. *       to demonstrate best Windows programming.                         *
  14. *                                                                        *
  15. **************************************************************************/
  16. /*
  17.     1. Open the new-project menu with  Project/New.
  18.     2. Call the project VDEMOW2.
  19.     3.a) 32-bit: Choose Application[EXE] for Win32.
  20.     3.b) 16-bit: Choose Application[EXE] for Windows3.x, memory model LARGE.
  21.     4. Be sure, CheckBox for the inclusion of OWL is checked.
  22.     5. Hit OK.
  23.     6. In the Project window that will now be on the screen, delete the
  24.        nodes VDEMOW2.DEF and VDEMOW2.RC.
  25.     7.a) 32-bit: Add the node VCF3W.LIB.
  26.     7.b) 16-bit: Add the node VCL3W.LIB.
  27.     8. In Options/Project, be sure the include-file search path and the
  28.        library search path both include the respective  OPTIVEC directories,
  29.        e.g.: \bc\optivec\include and bc\optivec\lib, resprectively
  30.     9. Compile and run.
  31. */
  32.  
  33. #include <owl\owlpch.h>
  34. #include <owl\applicat.h>
  35. #include <owl\framewin.h>
  36.  
  37. #include <VFstd.h>
  38. #include <VCFstd.h>
  39. #include <VFmath.h>
  40. #include <VCFmath.h>
  41. #include <VIstd.h>
  42. #include <Vgraph.h>
  43. #include <math.h>
  44. #if __BORLANDC__ < 0x500
  45.     #define BCBOOL BOOL
  46. #else
  47.     #define BCBOOL bool
  48. #endif
  49. NEWMATHERR
  50.  
  51. class TVecWindow : public TWindow
  52. {
  53.   public:
  54.      fVector  X1, X2, Y1, Y2, Y3, Y4, Spc, Freq, Win;
  55.      cfVector CX1, CX2;
  56.      iVector  I1;
  57.      ui       vsize, spcsize;
  58.      int      vview;
  59.      struct   time tStart, tStop;
  60.      TVecWindow(TWindow *parent = 0);
  61.     ~TVecWindow()
  62.        { V_nfree( 12, I1, X1, X2, Y1, Y2, Y3, Y4, CX1, CX2, Spc, Freq, Win );  }
  63.      void   StartTime( void );
  64.      double StopTime( void );
  65.      void EvRButtonDown(UINT, TPoint&);
  66.      void Paint(TDC&, BCBOOL, TRect&);
  67.      DECLARE_RESPONSE_TABLE(TVecWindow);
  68. };
  69.  
  70. DEFINE_RESPONSE_TABLE1(TVecWindow, TWindow)
  71.   EV_WM_RBUTTONDOWN,
  72. END_RESPONSE_TABLE;
  73.  
  74. TVecWindow::TVecWindow(TWindow *parent)
  75. {
  76.   Init(parent, 0, 0);
  77.   vsize  = 1025;
  78.   spcsize = 128;
  79.   vview  =    0;
  80.   V_setErrorEventFile( "VDEMO.LOG", 1 ); // save all VectorLib error messages
  81.   I1 = VI_vector( vsize );
  82.   X1 = VF_vector( vsize );
  83.   X2 = VF_vector( vsize );
  84.   Y1 = VF_vector( vsize );
  85.   Y2 = VF_vector( vsize );
  86.   Y3 = VF_vector( vsize );
  87.   Y4 = VF_vector( vsize );
  88.   Spc= VF_vector( spcsize+1 );
  89.   Freq=VF_vector( spcsize+1 );
  90.   Win= VF_vector( 2*spcsize );
  91.   CX1 = VCF_vector( vsize );
  92.   CX2 = VCF_vector( vsize );
  93. }
  94.  
  95.  
  96. void TVecWindow::EvRButtonDown(UINT, TPoint&)
  97. {
  98.   switch( vview )
  99.   {
  100.      default: vview=-1;
  101.      case 0:  VF_ramp( X1, vsize, 0, 80*M_PI/vsize );
  102.               VF_ramp( Freq, spcsize+1, 0, (vsize / (80*M_PI)) / spcsize );
  103.                 /* get a time axis from 0 to 80 Pi and a corresponding
  104.                    frequency axis from 0 to the Nyquist frequency */
  105.               VF_sin( Y1, X1, vsize );
  106.                     //  get a simple sine wave
  107.               break;
  108.      case 1:  VF_cmp_gtC( Y2, Y1, vsize, 0.7 );
  109.                     //  transform the sine into an asymmetric square wave
  110.               break;
  111.      case 2:  VF_Hanning( Win, 2*spcsize );
  112.               Spc[spcsize] = VFs_spectrum( Spc, spcsize, Y2, vsize, Win );
  113.                     //  analyse the frequency spectrum of the square wave
  114.               break;
  115.      case 3:  VI_ramp( I1, vsize, 0, 1 );
  116.               VF_ramp( X1, vsize, 0, 4.0 /(vsize-1) );
  117.               VF_sinrpi2( Y1, I1, vsize, (vsize-1)/2);
  118.               VFx_equV( Y1, Y1, vsize, 10.0, 20.0 );
  119.                    // get a sine wave, magnify it by a factor of 10
  120.                    // and displace it vertically by +20 units.
  121.               VF_tanrpi2( Y2, I1, vsize, (vsize-1)/2);
  122.               VF_limit( Y2, Y2, vsize, -40, 40 );
  123.                    //  limit the range of tangent values
  124.               VF_cosecrpi2( Y3, I1, vsize, (vsize-1)/2);
  125.               VF_limit( Y3, Y3, vsize, -40, 40 );
  126.                     //  the same for cosecant values
  127.               VF_subC( Y3, Y3, vsize, 20.0 );
  128.                     //  displace the cosecant by -20 units.
  129.               break;
  130.      case 4:  VCF_ramp( CX1, vsize, fcplx( M_PI, 0),
  131.                                     fcplx(0.4, 0.008) );
  132.               VCF_cos( CX2, CX1, vsize );
  133.               VCF_sin( CX1, CX1, vsize );
  134.                     //  Try also with other complex functions!
  135.               break;
  136.      case 5:  break;
  137.   }
  138.   vview++;
  139.   Invalidate();
  140. }
  141.  
  142. void TVecWindow::Paint(TDC& DC, BCBOOL, TRect&)
  143. {
  144.    char      dum[100];
  145.    unsigned  i, j;
  146.    float     xt, A=1.2, B=-0.13, C=0.85;
  147.  
  148.    V_initPlot( HWindow, DC );  // HWindow is the handle to the current Window
  149.    switch( vview )
  150.    {
  151.        default: break;
  152.        case 0: TextOut( DC, 10, 10,
  153.                "This is a series of graphs illustrating VectorLib functions.", 60 );
  154.                TextOut( DC, 10, 40,
  155.                "Always press the right mouse button to see the next view!", 56 );
  156.                TextOut( DC, 10, 70,
  157.                "Press [Alt] [F4] to end the demonstration.", 41 );
  158.                break;
  159.        case 1: TextOut( DC, 0, 10, "Sine wave", 9 );
  160.                V_drawAxes( VF_min( X1, vsize/2 ), VF_max( X1, vsize/2 ),
  161.                            -1.0, +1.0 );
  162.                VF_xyDataPlot( X1, Y1, vsize/2, PS_SOLID, LIGHTRED );
  163.                           // show only half of all waves
  164.                break;
  165.        case 2: TextOut( DC, 0, 10, "Asymmetric", 10 );
  166.                TextOut( DC, 0, 30, "square wave", 11 );
  167.                V_drawAxes( 0, vsize, -0.3, 1.2 );
  168.                VF_yDataPlot( Y2, vsize, PS_SOLID, BLUE );
  169.                break;
  170.        case 3: TextOut( DC, 0,  10, "Frequency spectrum", 18 );
  171.                TextOut( DC, 0,  30, "of the square wave", 18 );
  172.                TextOut( DC, 0, 150, "Don't be sur-", 13 );
  173.                TextOut( DC, 0, 170, "prised; the next", 16 );
  174.                TextOut( DC, 0, 190, "example will show", 17 );
  175.                TextOut( DC, 0, 210, "a bit of error", 14 );
  176.                TextOut( DC, 0, 230, "handling!", 9 );
  177.                VF_xyAutoPlot( Freq, Spc, spcsize+1, PS_SOLID | SY_CROSS, GREEN );
  178.                break;
  179.        case 4: TextOut( DC, 0,  10, "Trigonometric", 13 );
  180.                TextOut( DC,10,  30, "functions:", 10 );
  181.                TextOut( DC, 0,  50, "Red:   sine", 11 );
  182.                TextOut( DC, 0,  70, "Green: tangent", 14 );
  183.                TextOut( DC, 0,  90, "Blue:  cosecant", 15 );
  184.                TextOut( DC, 0, 120, "(sine:", 6 );
  185.                TextOut( DC, 0, 140, "   *10 and +20,", 15 );
  186.                TextOut( DC, 0, 160, " cosecant: -20)", 15 );
  187.                VF_xy2AutoPlot( X1, Y2, vsize, PS_SOLID, LIGHTGREEN,
  188.                                X1, Y3, vsize, PS_SOLID, LIGHTBLUE );
  189.                VF_xyDataPlot( X1, Y1, vsize, PS_SOLID, LIGHTRED );
  190.                break;
  191.        case 5: TextOut( DC, 0,  10, "Playing with functions", 22 );
  192.                TextOut( DC, 0,  30, "in the complex plane:", 21 );
  193.                TextOut( DC, 0,  60, "Red:   complex sine",  19 );
  194.                TextOut( DC, 0,  80, "Green: complex cosine",  21 );
  195.                VCF_2AutoPlot( CX1, vsize, PS_SOLID, LIGHTRED,
  196.                               CX2, vsize, PS_SOLID, LIGHTGREEN );
  197.                break;
  198.        case 6: TextOut( DC, 0, 10, "To end the demo, let's make a crude speed comparison", 52 );
  199.                TextOut( DC, 0, 30, "between compiled code and VectorLib code.", 41 );
  200.                V_nfree( 2, X1, X2 );
  201.                X1 = VF_vector( 4096 );
  202.                VF_ramp( X1, 4096, -10, 0.005 );
  203.                X2 = VF_vector( 4096 );
  204.                TextOut( DC, 0, 55, "E.g., evaluate the exponential function for a whole vector", 58 );
  205.                TextOut( DC, 20, 80, "Y[i] = c * exp[ a * X[i] + b],  i=0,..,size-1", 45 );
  206.                TextOut( DC, 0, 105, "(20 runs of 4096 elements each).   Please wait...", 49 );
  207.                StartTime();
  208.                    for( i=0; i<20; i++ )
  209.                       for( j=0; j<4096; j++ )
  210.                          X2[j] = C * exp( A * X1[j] + B );
  211.                xt = StopTime();
  212.                sprintf( dum, "Execution Time for classic loop version:  %lf sec", xt );
  213.                TextOut( DC, 0, 130, dum, strlen( dum ));
  214.                StartTime();
  215.                    for( i=0; i<20; i++ )
  216.                          VFx_exp( X2, X1, 4096, A, B, C );
  217.                xt = StopTime();
  218.                sprintf( dum, "Execution Time for VectorLib version:     %lf sec", xt );
  219.                TextOut( DC, 0, 155, dum, strlen( dum ));
  220.                TextOut( DC, 0, 190, "Generally, the VectorLib version is about 1.5 to 3 times faster than classic", 76 );
  221.                TextOut( DC, 0, 210, "loops; in some hardware environments, up to a factor of 8 can be gained!", 72 );
  222.        break;
  223.    }
  224. }
  225.  
  226. void TVecWindow::StartTime( void )
  227. {
  228.     gettime( &tStart );
  229. }
  230.  
  231. double TVecWindow::StopTime( void )
  232. {
  233.    double         tBegin, tEnd;
  234.    gettime( &tStop );
  235.    tBegin = (tStart.ti_hour * 60 + tStart.ti_min) * 60L
  236.             + tStart.ti_sec + tStart.ti_hund * 0.01;
  237.    tEnd   = (tStop.ti_hour * 60 + tStop.ti_min)  * 60L
  238.             + tStop.ti_sec + tStop.ti_hund * 0.01;
  239.  
  240.    return( tEnd - tBegin );
  241. }
  242.  
  243.  
  244. class TVecApp : public TApplication
  245. {
  246.   public:
  247.     TVecApp() : TApplication() {}
  248.  
  249.     void InitMainWindow()
  250.     {
  251.       nCmdShow = SW_SHOWMAXIMIZED;
  252.       SetMainWindow(new TFrameWindow(0, "VectorLib Demo", new TVecWindow));
  253.     }
  254. };
  255.  
  256. int OwlMain(int /*argc*/, char* /*argv*/ [])
  257. {
  258.   return TVecApp().Run();
  259. }
  260.